package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.product.entity.*;
import com.ruicar.afs.cloud.product.enums.*;
import com.ruicar.afs.cloud.product.mapper.ProductUpdateMapper;
import com.ruicar.afs.cloud.product.service.*;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yaorui.zhang
 * @version 1.0
 * @date 2020-05-26
 */
@Service
@AllArgsConstructor
public class ProductUpdateServiceImpl extends ServiceImpl<ProductUpdateMapper, ProductUpdate> implements ProductUpdateService {

    //产品方案
    private final ProductPlanService productPlanService;
    private final ProductPlanAtomService productPlanAtomService;
    private final ProductPlanRateService productPlanRateService;
    private final ProductPlanRepayService productPlanRepayService;
    //产品修改
    private final ProductUpdatePlanService productUpdatePlanService;
    private final ProductUpdateAtomService productUpdateAtomService;
    private final ProductUpdateRateService productUpdateRateService;
    private final ProductUpdateRepayService productUpdateRepayService;

    /**
     * @Description 产品批量修改定时任务
     */
    @Override
    public void timingTaskProductUpdate() {

        //查询审核完成的批量修改表
        List<ProductUpdate> productUpdateList = this.list(Wrappers.<ProductUpdate>query().lambda()
                .eq(ProductUpdate::getStatus, AfsEnumUtil.key(StatusType.TOREVIEW))//查询审核完成的审批表
                .le(ProductUpdate::getEffectiveDate, DateUtil.date())//生效时间小于等于当前时间
        );

        //循环处理到了生效期的数据
        for (ProductUpdate productUpdate : productUpdateList) {

            List<ProductUpdatePlan> productUpdatePlanList = productUpdatePlanService.list(Wrappers.<ProductUpdatePlan>query().lambda()
                    .eq(ProductUpdatePlan::getProductUpdateId, productUpdate.getId())//根据ID查询
            );
            for(ProductUpdatePlan productUpdatePlan:productUpdatePlanList){

                //产品方案ID
                Long productPlanId = productUpdatePlan.getProductPlanId();

                //产品方案复制
                ProductPlan productPlan = productPlanService.getById(productPlanId);

                //根据产品编号获取最大编号信息
                ProductPlan productPlanMax = productPlanService.getOne(Wrappers.<ProductPlan>query().lambda()
                        .eq(ProductPlan::getProductNumber,productPlan.getProductNumber())
                        .orderByDesc(ProductPlan::getVersionNumber)
                        , false
                );
                //所有编号产品下线
                productPlanService.update(Wrappers.<ProductPlan>lambdaUpdate()
                        .eq(ProductPlan::getProductNumber,productPlan.getProductNumber())
                        .set(ProductPlan::getStatus, AfsEnumUtil.key(ProductOnlineStatus.OFFLINE))
                );

                productPlan.setVersionNumber(productPlanMax.getVersionNumber()+1);
                productPlan.setId(null);
                productPlan.setCreateBy(null);
                productPlan.setCreateTime(null);
                productPlan.setUpdateBy(null);
                productPlan.setUpdateTime(null);
                productPlan.setStatus(AfsEnumUtil.key(ProductOnlineStatus.GO_ONLINE));
                //结束时间
                if(productUpdate.getEndTime()!=null && StringUtils.isNotBlank(productUpdate.getEndTime().toString())){
                    productPlan.setEndTime(productUpdate.getEndTime());
                }
                //投放方案
                if(productUpdate.getLaunchPlanId()!=null && StringUtils.isNotBlank(productUpdate.getLaunchPlanId().toString())){
                    productPlan.setLaunchPlanId(productUpdate.getLaunchPlanId());
                    productPlan.setLaunchPlanName(productUpdate.getLaunchPlanName());
                }
                //融资方案
                if(productUpdate.getFinancePlanId()!=null && StringUtils.isNotBlank(productUpdate.getFinancePlanId().toString())){
                    productPlan.setFinancePlanId(productUpdate.getFinancePlanId());
                    productPlan.setFinancePlanName(productUpdate.getFinancePlanName());
                }
                //贴息方案
                if(productUpdate.getDiscountPlanId()!=null && StringUtils.isNotBlank(productUpdate.getDiscountPlanId().toString())){
                    productPlan.setDiscountPlanId(productUpdate.getDiscountPlanId());
                    productPlan.setDiscountPlanName(productUpdate.getDiscountPlanName());
                }
                //附加方案
                if(productUpdate.getExtrasPlanId()!=null && StringUtils.isNotBlank(productUpdate.getExtrasPlanId().toString())){
                    productPlan.setExtrasPlanId(productUpdate.getExtrasPlanId());
                    productPlan.setExtrasPlanName(productUpdate.getExtrasPlanName());
                }
                //风险方案
                if(productUpdate.getRiskPlanId()!=null && StringUtils.isNotBlank(productUpdate.getRiskPlanId().toString())){
                    productPlan.setRiskPlanId(productUpdate.getRiskPlanId());
                    productPlan.setRiskPlanName(productUpdate.getRiskPlanName());
                }
                //保存新的产品方案信息
                productPlanService.save(productPlan);

                //得到最新的产品ID
                Long productPlanIdNew = productPlan.getId();

                //产品方案原子复制
                List<ProductPlanAtom> productPlanAtomList = productPlanAtomService.list(Wrappers.<ProductPlanAtom>query().lambda()
                        .eq(ProductPlanAtom::getProductPlanId, productPlanId)
                );
                for(ProductPlanAtom productPlanAtom:productPlanAtomList){
                    productPlanAtom.setId(null);
                    productPlanAtom.setProductPlanId(productPlanIdNew);
                    productPlanAtomService.save(productPlanAtom);
                }
                //产品修改原子
                List<ProductUpdateAtom> productUpdateAtomList = productUpdateAtomService.list(Wrappers.<ProductUpdateAtom>query().lambda()
                        .eq(ProductUpdateAtom::getProductUpdateId, productUpdate.getId())
                );
                for(ProductUpdateAtom productUpdateAtom:productUpdateAtomList){
                    if(StringUtils.isNotBlank(productUpdateAtom.getAtomValue())){
                        ProductPlanAtom productPlanAtomCopy = productPlanAtomService.getOne(Wrappers.<ProductPlanAtom>query().lambda()
                                        .eq(ProductPlanAtom::getProductPlanId,productPlanIdNew)
                                        .eq(ProductPlanAtom::getProductAtomKey,productUpdateAtom.getProductAtomKey())
                                ,false
                        );
                        productPlanAtomCopy.setAtomValue(productUpdateAtom.getAtomValue());
                        productPlanAtomService.updateById(productPlanAtomCopy);
                    }
                    //贴息方案
                    if(AfsEnumUtil.key(ProductAtomEnum.DISCOUNTTYPE).equals(productUpdateAtom.getProductAtomKey()) && AfsEnumUtil.key(DiscountCategory.NO).equals(productUpdateAtom.getAtomValue())){
                        productPlan.setDiscountPlanId(null);
                        productPlan.setDiscountPlanName(null);
                    }
                    //附加方案
                    if(AfsEnumUtil.key(ProductAtomEnum.ISADDLOAN).equals(productUpdateAtom.getProductAtomKey()) && AfsEnumUtil.key(StatusEnum.NO).equals(productUpdateAtom.getAtomValue())){
                        productPlan.setExtrasPlanId(null);
                        productPlan.setExtrasPlanName(null);
                    }
                    //是否风险定价
                    if(AfsEnumUtil.key(ProductAtomEnum.ISRISKPRICING).equals(productUpdateAtom.getProductAtomKey()) && AfsEnumUtil.key(StatusEnum.NO).equals(productUpdateAtom.getAtomValue())){
                        productPlan.setRiskPlanId(null);
                        productPlan.setRiskPlanName(null);
                    }
                }
                //产品利率复制
                List<ProductUpdateRate> productUpdateRateList = productUpdateRateService.list(Wrappers.<ProductUpdateRate>query().lambda()
                        .eq(ProductUpdateRate::getProductUpdateId, productUpdate.getId())
                );
                if(productUpdateRateList.size()>0){
                    //产品修改利率复制
                    for(ProductUpdateRate productUpdateRate:productUpdateRateList){
                        ProductPlanRate productPlanRate = new ProductPlanRate();
                        productPlanRate.setProductPlanId(productPlanIdNew);
                        productPlanRate.setClearRate(productUpdateRate.getClearRate());
                        productPlanRate.setCustRate(productUpdateRate.getCustRate());
                        productPlanRate.setLoanPeriodEnd(productUpdateRate.getLoanPeriodEnd());
                        productPlanRate.setLoanPeriodStart(productUpdateRate.getLoanPeriodStart());
                        productPlanRate.setAddPointValue(productUpdateRate.getAddPointValue());
                        productPlanRateService.save(productPlanRate);
                    }
                }else{
                    //产品方案利率复制
                    List<ProductPlanRate> productPlanRateList = productPlanRateService.list(Wrappers.<ProductPlanRate>query().lambda()
                            .eq(ProductPlanRate::getProductPlanId, productPlanId)
                    );
                    for(ProductPlanRate productPlanRate:productPlanRateList){
                        productPlanRate.setId(null);
                        productPlanRate.setProductPlanId(productPlanIdNew);//新的ID
                        productPlanRateService.save(productPlanRate);
                    }
                }

                //产品还款方式复制
                ProductUpdateRepay productUpdateRepay = productUpdateRepayService.getOne(Wrappers.<ProductUpdateRepay>query().lambda()
                        .eq(ProductUpdateRepay::getProductUpdateId, productUpdate.getId())
                        ,false
                );
                if(productUpdateRepay!=null && StringUtils.isNotEmpty(productUpdateRepay.getRepaymentMethod())){
                    //产品修改还款方式复制
                    ProductPlanRepay productPlanRepay = new ProductPlanRepay();
                    productPlanRepay.setProductPlanId(productPlanIdNew);
                    productPlanRepay.setRepaymentMethodData(productUpdateRepay.getRepaymentMethodData());
                    productPlanRepay.setRepaymentMethod(productUpdateRepay.getRepaymentMethod());
                    productPlanRepayService.save(productPlanRepay);
                }else{
                    //产品方案还款方式复制
                    List<ProductPlanRepay> productPlanRateList = productPlanRepayService.list(Wrappers.<ProductPlanRepay>query().lambda()
                            .eq(ProductPlanRepay::getProductPlanId, productPlanId)
                    );
                    for(ProductPlanRepay productPlanRepay:productPlanRateList){
                        productPlanRepay.setId(null);
                        productPlanRepay.setProductPlanId(productPlanIdNew);//新的ID
                        productPlanRepayService.save(productPlanRepay);
                    }
                }

                //重新保存产品方案信息
                productPlanService.saveOrUpdate(productPlan);
                //佣金系统推送MQ
                productPlanService.sendAlterDealerMq(productPlanIdNew);
            }

            productUpdate.setStatus(AfsEnumUtil.key(StatusType.BELAUNCH));
            this.updateById(productUpdate);
        }
    }

}
